package vn.codespot.hms.utils;

import vn.codespot.hms.test.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils.TruncateAt;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NavigationBar extends RelativeLayout implements OnClickListener {
    public static final int NAVIGATION_BUTTON_LEFT = 0;
    public static final int NAVIGATION_BUTTON_RIGHT = 1;
    public static final int NAVIGATION_BUTTON_CENTER = 2;
    Context mContext;
    NavigationBarListener mListener;

    public NavigationBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mContext = context;
        init();
    }

    public void init() {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-1, -2);
        this.setLayoutParams(lp);
        this.setBackgroundResource(R.drawable.navigation_bg);
    }

    public void setButtonImageLeft(Integer normal, Integer selected, Integer pressed) {
        setButtonImage(normal, selected, pressed, NAVIGATION_BUTTON_LEFT);
    };

    public void setButtonImageRight(Integer normal, Integer selected, Integer pressed) {
        setButtonImage(normal, selected, pressed, NAVIGATION_BUTTON_RIGHT);
    };

    public void setButtonImageCenter(Integer normal, Integer selected, Integer pressed) {
        setButtonImage(normal, selected, pressed, NAVIGATION_BUTTON_CENTER);
    };

    private void setButtonImage(Integer normal, Integer selected, Integer pressed, int which) {
        MyButton oldButton = (MyButton) findViewWithTag(Integer.valueOf(which));
        if (oldButton != null) {
            this.removeView(oldButton);
        }

        // create the new button
        MyButton newButton = new MyButton(mContext);
        newButton.setTag(Integer.valueOf(which));

        // set OnClickListener
        newButton.setOnClickListener(this);

        // set LayoutParams

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(60, 60);
        if (which == NAVIGATION_BUTTON_LEFT) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        } else if (which == NAVIGATION_BUTTON_RIGHT) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        } else if (which == NAVIGATION_BUTTON_CENTER) {
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        }
        lp.addRule(RelativeLayout.CENTER_VERTICAL);

        // lp.setMargins(10, 30, 10, 30);
        lp.setMargins(0, 10, 0, 10);
        newButton.setLayoutParams(lp);

        newButton.setPressedBtn(normal, pressed, selected);

        // add button to view
        this.addView(newButton);
    }

    public void setTitleBar(String title) {
        // remove the old title if exists
        TextView oldTitle = (TextView) findViewWithTag("navTitle");
        if (oldTitle != null) {
            this.removeView(oldTitle);
        }

        // create the new title
        TextView newTitle = new TextView(mContext);
        newTitle.setTag("navTitle");

        // set LayputParams
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-2, -2);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        lp.setMargins(60, 0, 60, 0);
        newTitle.setLayoutParams(lp);

        // set text-size-color
        newTitle.setText(title);
        newTitle.setTextSize(25);
        newTitle.setTextColor(Color.WHITE);
        newTitle.setEllipsize(TruncateAt.END);
        newTitle.setMaxLines(1);
        newTitle.setShadowLayer(1, 1, 1, Color.BLACK);
        newTitle.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);

        // add title to navigation bar
        this.addView(newTitle);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int which = ((Integer) v.getTag()).intValue();
        if (mListener != null) {
            mListener.OnNavigationBarButtonClick(which);
        }

    }

    public void setNavigationBarListener(NavigationBarListener listener) {
        mListener = listener;
    }

    public interface NavigationBarListener {
        public void OnNavigationBarButtonClick(int which);
    }
}
